<?php

namespace CNEPAdmin\Controller;

use CNEPAdmin\Controller\CommonController;

class UserController extends CommonController {

    protected $_modelName = "User";

    protected function setCode() {
        $this->_code = "User";
    }

    public function index() {
        $this->display();
    }

    public function getAllUserList($fields = '*') {
        $where = array();
        $order = array();
        $this->_getQueryArr($where, $order);
//        dump($where);
//        exit();
        return D("User")->getAllUserList($where, $order, $fields);
    }
    
    /**
     * 得到条件查询数组
     * @return Array
     */
    protected function _getQueryArr(&$where, &$order) {

        if (strlen(trim(I("sPhone"))) > 0) {
            $where['phone'] = trim(I("sPhone"));
        }
        if (strlen(I("sNickName")) > 0) {
            $where['nickName'] = array('like', "%" . trim(I("sNickName", '')) . '%');
        }
        if (strlen(I("sChannelID")) > 0) {
            $where['channelID'] = trim(I("sChannelID"));
        }

        $where['isOn'] = 1;

        $orderBy = 'addTime';
        $desc = 'desc';
        if (intval(I("sDesc")) === 1) {
            $desc = 'asc';
        }
        if (strlen(trim(I("sOrderBy", ''))) > 0) {
            $orderBy = I("sOrderBy");
        }

        $order = array(
            $orderBy => $desc
        );

        return $where;
    }

    /**
     * 查看列表
     */
    public function userList() {
        $this->checkPriv('ur', 1, false); // 权限

        $this->_getOnePageData("getUserList", "getUserListLength");
    }

    public function userMsg() {
        $this->checkPriv('ur', 1, false);

        $id = intval(I("userID", 0));

        if ($id > 0) { // 编辑
            $arr = D("User")->where(array("isOn" => 1, 'userID' => $id))->find();
            if ($arr) {
                $arr['addTime'] = date("Y/m/d H:i:s", $arr['addTime']);
//                $arr['lastLogin'] = $arr['lastLogin'] > 0 ? date("Y/m/d H:i:s", $arr['lastLogin']) : '';
//                $arr['sexStr'] = intval($arr['sex']) === 0 ? "女" : "男";
//                $arr['canCommentStr'] = intval($arr['canComment']) === 1 ? "可评论" : "不可评论";
//                $arr['isFreezeStr'] = intval($arr['isFreeze']) === 1 ? "已冻结" : "否";
				$headimg = trim($arr['headImg']);
				$tmpl = C("TMPL_PARSE_STRING");
                $arr['headImg'] = empty($headimg) ? $tmpl['__STATICS__']."/images/user_logo.png" : $arr['headImg'];

                unset($arr['token']);
                unset($arr['password']);
                unset($arr['isOn']);
            }
        }

        $this->ajaxReturn($arr);
    }

    /**
     * 处理更新字段
     * @param type $dat
     * @return type
     */
    private function _dealUpdateField($dat = '') {
        if (empty($dat)) {
            $dat = I("post.");
        }
        if (isset($dat['phone'])) {
            $data['phone'] = trim($dat['phone']);
        }
        if (isset($dat['email'])) {
            $data['email'] = trim($dat['email']);
        }
        if (isset($dat['errorMsg'])) {
            $data['errorMsg'] = trim($dat['errorMsg']);
        }
        if (isset($dat['canComment']) &&
                (intval($dat['canComment']) === 0 || intval($dat['canComment']) === 1)) {
            $data['canComment'] = intval($dat['canComment']);
        }
        if (isset($dat['nickName'])) {
            $data['nickName'] = trim($dat['nickName']);
        }
        if (isset($dat['isFreeze']) &&
                (intval($dat['isFreeze']) === 0 || intval($dat['isFreeze']) === 1)) {
            $data['isFreeze'] = intval($dat['isFreeze']);
        }
        $data['updateTime'] = time();
        return $data;
    }
    
    /**
     * 更新基本信息(需要更新什么就提交什么)
     */
    public function updateUserMsg() {
        $this->checkPriv("ur", 2);

        $userID = intval(I("post.userID"));

        if ($userID > 0) {
            // 判断是否已删除
            $isOn = D("User")->where("userID=$userID")->getField('isOn');
            if (intval($isOn) === 1) {
                // 未删除的话修改
//                $data = $this->_dealUpdateField(I("post."));
//                $data['userID'] = $userID;
                
                $this->_ajaxOperate('updateUser', '', '', I("post."));
            } else {
                $this->ajaxReturn(array(
                    "result" => 0, "desc" => "用户不存在"
                ));
            }
        } else {
            $this->ajaxReturn(array(
                "result" => 0, "desc" => "无效ID"
            ));
        }
    }

    /**
     * 删除
     */
    public function deleteUser() {
        $this->checkPriv("ur", 2);

        $userID = intval(I("post.userID"));

        if ($userID > 0) {
            $this->_ajaxOperate('deleteUser', '', '', $userID);
        } else {
            $this->ajaxReturn(array(
                "result" => 0, "desc" => "无效ID"
            ));
        }
    }

}
